package com.example.service.impl;

import com.example.domain.entity.News;
import com.example.mapper.HadoopMapper;
import com.example.mapper.NewsMapper;
import com.example.service.HadoopService;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * HadoopServiceImpl
 *
 * @Description TODO
 * @Author 泽儿_
 * @Date 10:56 2023/5/10
 **/
public class HadoopServiceImpl implements HadoopService {
    @Autowired
    private HadoopMapper hadoopMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private Configuration hadoopConfig;

    public void importDataFromHadoopToMySQL(String hadoopFilePath) throws Exception {
        FileSystem fileSystem = FileSystem.get((org.apache.hadoop.conf.Configuration) hadoopConfig);
        Path path = new Path(hadoopFilePath);
        if (!fileSystem.exists(path)) {
            throw new Exception("File not exists: " + hadoopFilePath);
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(fileSystem.open(path)));
        String line;
        while ((line = br.readLine()) != null) {
            String[] fields = line.split(",");
            News data = new News();
            data.setId(Integer.parseInt(fields[0]));
            data.setNewsTag(String.valueOf(Integer.parseInt(fields[1])));
            data.setTitle(fields[2]);
            data.setText(fields[3]);
            data.setClick(Integer.parseInt(fields[4]));
            data.setFinishPercent(Double.parseDouble(fields[5]));
            data.setPraise(Integer.parseInt(fields[6]));
            data.setUnlike(Integer.parseInt(fields[7]));
            newsMapper.insert(data);
        }
        br.close();
    }

}
